package sasp_messages;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;

public class CommCipher {

	private SecretKey key;
	private Cipher saspCipher;

	private void setKey(SecretKey _key)	{
		key = _key;
	}
	
	public SecretKey getKey()	{
		return key;
	}

	private void generateKey() throws NoSuchAlgorithmException	{
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		key = keyGen.generateKey();
	}

	public byte[] encrypt(String _m) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException	{
		saspCipher.init(Cipher.ENCRYPT_MODE, key);
		return saspCipher.doFinal(_m.getBytes());
	}

	public byte[] decrypt(String _c) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException	{
		saspCipher.init(Cipher.DECRYPT_MODE, key);
		return saspCipher.doFinal(_c.getBytes());
	}

        private void createCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
            saspCipher = Cipher.getInstance("AES");
        }

	public CommCipher(SecretKey _key)	{
		this.setKey(_key);
	}

	public CommCipher() throws NoSuchAlgorithmException	{
		this.generateKey();			
	}



}
